<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .tr {
            display: flex;
        }

        .tr .td {
            flex: 1;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: white;

        }

        .tr .td input {
            width: 100px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            <div>
                用户名 <input v-model="username"/>
            </div>
            <div>
                年龄 <input v-model="userage"/>
            </div>
            <button @click="add">新增</button>
        </div>

        <div class="tr">
            <div class="td">编号</div>
            <div class="td">用户名</div>
            <div class="td">年龄</div>
            <div class="td">操作</div>
        </div>
        
        <div class="tr" v-for="item in list">
            <div class="td">{{item.id}}</div>
            <div class="td">{{item.username}}</div>
            <div class="td">{{item.userage}}</div>
            <div class="td" @click="delById(item.id)">删除</div>
        </div>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            username:"",
            userage:"",
            list: []
        },
        created() {
            fetch("http://localhost:2345/userinfo").then(res => res.json()).then(res => {
                this.list = res;
            })
        },
        methods:{
           async add(){
            let res  = await  axios({
                method:"post",
                url:"http://localhost:2345/userinfo",
                data:{
                    username:this.username,
                    userage:this.userage
                }
            })
            // console.log(res);
            this.list.push(res.data);


            },
            delById(id){
                axios({
                    method:"delete",
                    url:"http://localhost:2345/userinfo/"+id
                }).then(()=>{
                    this.list = this.list.filter(item=>item.id!==id)
                })

            }
        }
    })
</script>

</html>